package www.bancodelanacion.intranet.prestamos.listeners;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

/**
 * Application Lifecycle Listener implementation class EscuchadorSesionAtributoCuentaSesiones
 *
 */
public class EscuchadorSesionAtributoCuentaSesiones implements HttpSessionAttributeListener {
	private SimpleDateFormat sdf = 	new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
   
    public void attributeAdded(HttpSessionBindingEvent arg0) {
    	ServletContext application = arg0.getSession().getServletContext();
    	if(arg0.getName().equals("TRABAJADOR_LOGEADO")){
    		application.setAttribute("FECHA_ULTIMO_INGRESADO", sdf.format(new Date()));
    		
    		if(application.getAttribute("INGRESADOS")  == null){
        		application.setAttribute("INGRESADOS", 1);
        	}else{
        		Integer contador = (Integer)application.getAttribute("INGRESADOS");
        		contador++;
        		application.setAttribute("INGRESADOS", contador);
        		
        	}
    	}
    	
    }
    public void attributeRemoved(HttpSessionBindingEvent arg0) {
    	ServletContext application = arg0.getSession().getServletContext();
    	
    	if(arg0.getName().equals("TRABAJADOR_LOGEADO")){
        	Integer contador = (Integer)application.getAttribute("INGRESADOS");
    		contador--;
    		application.setAttribute("INGRESADOS", contador);
    	}

    }
    public void attributeReplaced(HttpSessionBindingEvent arg0) {

    }
	
}
